package com.nus;

import java.util.ArrayList;

import com.nus.R.color;
import com.nus.datafeeder.Companies_Data;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CareerCenter extends Activity implements OnClickListener {
	public ImageButton home;
	public static Companies_Data cd;
	private ListView listview;
	private ArrayList<Companies_Data> mListItem;
	protected TextView title;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.careercenter);
		 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		 title = (TextView) findViewById(R.id.titleheader);
		 title.setText("Career Center");
		 ImageView b = (ImageView)findViewById(R.id.home);
		  b.setImageDrawable(getResources().getDrawable(R.drawable.careercenter));
//		 home = (ImageButton) findViewById(R.id.home);
//		 home.setOnClickListener(new OnClickListener() {
//		 
//		 @Override 
//		 public void onClick(View v) { // TODO Auto-generated method
//		  startActivity(new Intent("com.nus.OptionsActivity")); } });
		 
		boolean netconn = checkInternetConnection();
		if (!netconn) {
			Toast.makeText(getApplicationContext(),
					"Please check your internet or 3G connection",
					Toast.LENGTH_LONG).show();
		}
		listview = (ListView) findViewById(R.id.list_view);

		mListItem = Companies_Data.getItems();
		listview.setAdapter(new ListAdapter(CareerCenter.this, R.id.rlt_main,
				mListItem));
	}

	@Override
	public void onClick(View v) {

	}

	// ***ListAdapter***

	private class ListAdapter extends ArrayAdapter<Companies_Data> { // --
		// CloneChangeRequired
		private ArrayList<Companies_Data> mList; // --CloneChangeRequired
		private Context mContext;

		public ListAdapter(Context context, int textViewResourceId,
				ArrayList<Companies_Data> list) { // --CloneChangeRequired
			super(context, textViewResourceId, list);
			this.mList = list;
			this.mContext = context;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			try {
				if (view == null) {
					LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					view = vi.inflate(R.layout.list_item2, null);
				}
				final Companies_Data listItem = mList.get(position);
				if (listItem != null) {
					// setting list_item views
					TextView tv = (TextView) view.findViewById(R.id.name);
					tv.setText(listItem.getName());
					TextView tv2 = (TextView) view.findViewById(R.id.desc);
					//tv2.setTextColor(Color.BLUE);
					tv2.setText(listItem.getDescription());
					view.setOnClickListener(new OnClickListener() {
						public void onClick(View arg0) { // --clickOnListItem
							Intent myIntent = new Intent(CareerCenter.this,
									CareerCenterDetails.class);
                            arg0.setPressed(true);
							myIntent.putExtra("NAME", listItem.getName()
									.toString());
							myIntent.putExtra("DESC", listItem.getDescription());
							myIntent.putExtra("TIME", listItem.getTime());
							myIntent.putExtra("DTEXT",
									listItem.getDetailedText());
							startActivity(myIntent);
							// finish();
							arg0.setBackgroundColor(Color.parseColor("#191919"));
						}
					});
                        view.setOnTouchListener(new OnTouchListener() {
						
						public boolean onTouch(View v, MotionEvent event) {
							// TODO Auto-generated method stub
						
								v.setBackgroundColor(Color.BLUE);
							
								//v.performClick();
						
							
							
							return false;
						}
					});
				}
			} catch (Exception e) {
				Log.i(CareerCenter.ListAdapter.class.toString(), e.getMessage());
			}
			return view;
		}
	}

	public boolean checkInternetConnection() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isAvailable()
				&& cm.getActiveNetworkInfo().isConnected()) {
			return true;

		} else {
			return false;
		}
	}
}